import fs from 'fs';
import { fileURLToPath } from 'url'
import path from 'path'
import ts from 'rollup-plugin-typescript2'
import json from '@rollup/plugin-json'
import resolve from '@rollup/plugin-node-resolve'
import commonjs from '@rollup/plugin-commonjs'
import babel from '@rollup/plugin-babel'
import livereload from 'rollup-plugin-livereload'
import serve from 'rollup-plugin-serve'
const __dirname = path.dirname(fileURLToPath(import.meta.url));
// import eslint from '@rollup/plugin-eslint'
export default fs.readdirSync(path.resolve(__dirname,'packages')).filter(dir => fs.statSync(path.resolve(__dirname, 'packages', dir)).isDirectory())
  .map(dir => {
    // import pkg from './package.json'
    var filepath = path.resolve(__dirname, 'packages', dir);
    const pkg = fs.readFileSync(path.resolve(filepath, 'package.json'));
    return {
      input: path.resolve(filepath, 'src/index.ts'),
      output: [
        {
          file: pkg.main,
          format: 'cjs',
          sourcemap: 'inline',
        },
        {
          file: pkg.module,
          format: 'esm',
          sourcemap: 'inline',
        },
        {
          file: pkg.jsdelivr,
          format: 'umd',
          name: pkg.name,
          sourcemap: 'inline',
        }
      ],
      plugins: [
        resolve(),
        commonjs(),
        json(),
        // eslint({
        //   throwOnError: true,
        //   throwOnWarning: true,
        //   include: ['src/**'],
        //   exclude: ['node_modules/**']
        // }),
        ts({
          tsconfig: path.resolve(__dirname, './tsconfig.json'),
          extensions: ['.js', '.ts']
        }),
        babel({
          babelHelpers: true,
          exclude: 'node_modules/**',
        }),
        livereload(),
        serve({
          open: true,
          port: 13000,
          contentBase: './',
          openPage: '/examples/index.html'
        }),
      ]
    }
  })
